package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyFactoryFav;
import zhihuilan.entity.MyPunishment;
import zhihuilan.entity.MyShopMember;
import zhihuilan.entity.MyShopMember;
import zhihuilan.mapper.MyShopMemberMapper;
import zhihuilan.params.query.MyShopMemberQueryParam;
import zhihuilan.service.MyShopMemberService;

import java.util.List;

/**
 * 部门管理 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyShopMemberServiceImpl extends ServiceImpl<MyShopMemberMapper, MyShopMember> implements MyShopMemberService {

    @Override
    public WxMiniPageDto<MyShopMember> selectPageList(MyShopMemberQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyShopMember> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
            queryWrapper.eq("staffid", queryParam.getStaffid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
            queryWrapper.eq("status", queryParam.getStatus());
        } else {
            queryWrapper.eq("status", 0);
        }
        queryWrapper.orderByDesc("addtime");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShopMember> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyShopMember> selectList = new WxMiniPageDto<>(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, selectList);
        selectList.setData(page.getRecords());
        return selectList;
    }

    @Override
    public List<MyShopMember> selectList(MyShopMemberQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyShopMember> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
            queryWrapper.eq("staffid", queryParam.getStaffid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
            queryWrapper.eq("status", queryParam.getStatus());
        }
        queryWrapper.orderByDesc("addtime");
        List<MyShopMember> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    /**
     * 获取工厂会员总数；
     */
    @Override
    public Long selectCount(MyShopMemberQueryParam queryParam) {
        QueryWrapper<MyShopMember> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return 0L;
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
            queryWrapper.eq("staffid", queryParam.getStaffid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
            queryWrapper.eq("status", queryParam.getStatus());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
            queryWrapper.eq("status", queryParam.getStatus());
        }
        queryWrapper.orderByDesc("id");
        Long result = this.baseMapper.selectCount(queryWrapper);
        return result;
    }

    /**
     * 根据Id获取工厂会员列表；
     */
    @Override
    public MyShopMember selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        MyShopMember myShopMember = this.baseMapper.selectById(id);
        return myShopMember;
    }

    /**
     * 新增工厂会员；
     */
    @Override
    public MyShopMember addEntity(MyShopMember entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    /**
     * 更新工厂会员
     */
    @Override
    public MyShopMember updateEntity(MyShopMember entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deleteEntity(Integer id){
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int count = this.baseMapper.deleteById(id);
        return count>0;
    }
}
